﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace Service.Codes
{
    /// <summary>
    /// 上传文件到ftp(hdfs)
    /// </summary>
    public class WinProp_UploadFtp
    {
        private NLog.Logger logger = new NLog.LogFactory().GetCurrentClassLogger();

        /// <summary>
        /// 上传fpd文件到ftp（hdfs）上
        /// </summary>
        /// <param name="fpdPath">本地fpd文件路径</param>
        /// <param name="ftpUser">ftp账户</param>
        /// <param name="ftpPassword">ftp密码</param>
        /// <param name="ftpIP">ftp ip地址</param>
        /// <param name="ftpPort">ftp端口</param>
        /// <param name="ftpDir">ftp服务器将要进入的目录</param>
        public void Execute(string fpdPath, string city, string ftpUser = "jc_rc_ftp", string ftpPassword = "Rc@12345", string ftpIP = "10.78.142.179", string ftpPort = "22021", string ftpDir = "/jc_rc/rc_hive_db/fpdTest/")
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("@echo off");
            sb.AppendLine(string.Format("set ftpUser={0}", ftpUser));
            sb.AppendLine(string.Format("set ftpPass={0}", ftpPassword));
            sb.AppendLine(string.Format("set ftpIP={0}", ftpIP));
            sb.AppendLine(string.Format("set ftpPort={0}", ftpPort));
            sb.AppendLine(string.Format("set ftpFolder={0}", ftpDir + city + "/"));
            sb.AppendLine(string.Format("set LocalFolder={0}", fpdPath));

            sb.AppendLine("set ftpFile=%~dp0tempftp.txt");
            sb.AppendLine(">\"%ftpFile%\" (");
            sb.AppendLine("echo open %ftpIP% %ftpPort%");
            sb.AppendLine("echo,%ftpUser%");
            sb.AppendLine("echo,%ftpPass%");
            sb.AppendLine("echo mkdir /jc_rc/rc_hive_db/fpdTest/"+ city);
            sb.AppendLine("echo cd %ftpFolder%");
            sb.AppendLine("echo lcd %LocalFolder%");
            sb.AppendLine("echo bin");
            sb.AppendLine("echo mput **");
            sb.AppendLine("echo bye");
            sb.AppendLine(")");

            sb.AppendLine("echo %ftpFile%");
            sb.AppendLine("\"C:\\Windows\\System32\\ftp.exe\" -v -i -s:\"%ftpFile%\" ");

            string filePath = System.AppDomain.CurrentDomain.BaseDirectory + "\\testChange.bat";
            if (!File.Exists(filePath))
            {
                FileStream fs1 = new FileStream(filePath, FileMode.Create, FileAccess.Write);//创建写入文件
                StreamWriter sw = new StreamWriter(fs1);
                sw.WriteLine(sb);//开始写入值
                sw.Close();
                fs1.Close();
            }
            else
            {
                FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Write);
                StreamWriter sr = new StreamWriter(fs);
                sr.WriteLine(sb);//开始写入值
                sr.Close();
                fs.Close();
            }

            executeBat(System.AppDomain.CurrentDomain.BaseDirectory);
        }

        private void executeBat(string nodePath)
        {
            Process proc = null;
            try
            {
                proc = new Process();
                proc.StartInfo.WorkingDirectory = string.Format(nodePath);
                proc.StartInfo.FileName = "testChange.bat";
                proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//这里设置DOS窗口不显示，经实践可行
                proc.Start();
                proc.WaitForExit();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString());
            }
        }
    }
}
